package dashboard.common.pool;

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

public class PooledStringBufferFactory extends BasePooledObjectFactory<StringBuffer> {

    @Override
    public StringBuffer create() throws Exception {
        return new StringBuffer(16).append(Math.random());
    }

    @Override
    public PooledObject<StringBuffer> wrap(StringBuffer stringBuffer) {
        return new DefaultPooledObject<StringBuffer>(stringBuffer);
    }
    
    public static void main(String[] args) throws Exception {
    	GenericObjectPoolConfig conf = new GenericObjectPoolConfig();
    	conf.setMaxTotal(20);
    	conf.setMaxIdle(10);
    	GenericObjectPool<StringBuffer> pool = new GenericObjectPool<StringBuffer>(new PooledStringBufferFactory(), conf);
    	 Object o = pool.borrowObject(); 
         System.out.println(o); 

         Object o1 = pool.borrowObject(); 
         System.out.println(o1); 

         Object o2 = pool.borrowObject(); 
         System.out.println(o2); 

         Object o3 = pool.borrowObject(); 
         System.out.println(o3); 

         Object o4 = pool.borrowObject(); 
         System.out.println(o4); 


         Object o5 = pool.borrowObject(); 
         System.out.println(o5); 


         Object o6 = pool.borrowObject(); 
         System.out.println(o6); 

         Object o7 = pool.borrowObject(); 
         System.out.println(o7); 

         Object o8 = pool.borrowObject(); 
         System.out.println(o8); 
//         CompositeConfiguration config = new CompositeConfiguration(); 
//         //config.addConfiguration(new SystemConfiguration()); 
//         config.addConfiguration(new PropertiesConfiguration("cfgtest/test1.properties"));
	}
}